package com.qf.controller;

import com.qf.service.ICacheService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/cache")
public class CacheController {
    private ICacheService iCacheService;
    @Autowired
    public void setiCacheService(ICacheService iCacheService) {
        this.iCacheService = iCacheService;
    }

    /*  将key 保存在redis 的String类型
    value 写成Object 的原因是  消费者可能会传递对象过来 需要转换成json 存入redis 中
    expireSecond 过期时间
    */
    @PostMapping("/save2cachewithexpire")
   public void save2Cache (String key,String value, Long expireSecond){

        iCacheService.save2Cache(key, value,expireSecond);
   };
    @PostMapping("/save2cache/{key}/{value}")
    //  从不过期的数据
    public  void save2Cache (@PathVariable String key,@PathVariable String value){

        iCacheService.save2Cache(key, value);
    };

    //   获取指定key 的值
    @GetMapping("/getobject/{key}")
    public  Object get(@PathVariable String key){
        return iCacheService.get(key);
    };

    //   获取指定key 的值
    @GetMapping("/get/{key}")
    public  Object getph(@PathVariable String key){
        return iCacheService.get(key);
    }



    /*    删除指定的key
        暂时写的是可变数组, 因为我们暂时还没有确定消费者传递的时候到底是传递集合方便还是可变数据方便
        可能是多个
    */
    @PostMapping("/delete/{keys}")
    public boolean delete(@PathVariable String keys){
        return iCacheService.delete(keys);
    };

    @RequestMapping(value = "/delete/{key}/{mobile}", method = RequestMethod.POST)
    public boolean del(@PathVariable String key,@PathVariable String mobile){
        return iCacheService.del(key,mobile);
    }


    //  给指定的key 设置有效时间      expire到期
    @PostMapping("/expire/{key}/{expireSecond}")
    public Boolean expire(@PathVariable String key,@PathVariable Integer expireSecond){
        return  iCacheService.expire(key, expireSecond);
    };


    //    自增的部长写负数的代表自减
    @GetMapping("/incr")
    public long incr( String key, long value){
        return   iCacheService.incr(key, value);
    };

    //从redis中获取指定key 的hash数据
    @GetMapping("/hmget/{key}")
    public  Map<Object,Object> hGet(@PathVariable String key){
      return   iCacheService.hGetAll(key);

    };

    //    向hash中添加一个属性数据
    @PostMapping("/hSet")
    public void hSet(String key,String field,String value){

        iCacheService.hSet(key, field, value);
    };

//  批量向redis指定key的hash中添加数据

    @PostMapping("/hmset/{key}")
    public void HSet(@PathVariable String key,@RequestBody Map map){
        iCacheService.HSet(key, map);

    };

    //  获取指定key  指定field 得值
    @GetMapping("/HGet/{key}/{field}")
    public String HGet(@PathVariable String key,@PathVariable String field){

        return iCacheService.HGet(key,field);
    };
    @GetMapping("/hincr")
    public Long hIncr(@RequestParam String key, @RequestParam String field,@RequestParam Long delta){
    return     iCacheService.hIncr(key, field, delta);
    }
    @PostMapping("/pipel")
    public List<Object> pipelineOps(@RequestBody Map<String, Object> map) {
        return iCacheService.pipelineOps(map);
    }


    @PostMapping("/add2set")
//    @PostMapping("/cache/add2set")
//@PostMapping("/cache/add2set/{key}/{members}")
     public Long add2set(@RequestParam("key") String key,@RequestParam("members")String... members){
//        将所有的黑名单  添加到缓存中
       return iCacheService.add2Set(key,members);
    }

//    查询黑名单中所有的号码
    @GetMapping("/members/{key}")
    public Set<Object> members(@PathVariable String key){
       return iCacheService.members(key);
    }
//    判断号码是否在黑名单中
    @GetMapping("/ismember/{key}/{member}")
    public Boolean isMesbers(@PathVariable String key,@PathVariable String member){
        return iCacheService.isMember(key, member);
    }

//    从缓存中获取  策略
    @GetMapping("/get/filter/{key}")
    public Set<Object> filter(@PathVariable String key){
        return iCacheService.filter(key);

    }

    @PostMapping("/zadd")
    public Boolean add2Zent(String key, String value, double score){
        return  iCacheService.add2Zset(key, value, score);
    }
    @GetMapping("/zrange")
    public Long getSizeByScore(@RequestParam String key,@RequestParam  double min,@RequestParam   double max){
        Long sizeByScore = iCacheService.getSizeByScore(key, min, max);
        return sizeByScore;
    };


    @GetMapping("/rangewithscores")
    Set<ZSetOperations.TypedTuple<Object>> getDataFromZsetByIndex(String key,int start,int end){
        Set<ZSetOperations.TypedTuple<Object>> dataFromZset = iCacheService.getDataFromZset(key, start, end);
        return dataFromZset;
    }

    @PostMapping("/rpush")
    Long rPush(String key,String...values){
        return iCacheService.rPush(key, values);
    }
    //    从 List 中获取指定位置的数据
    @GetMapping("/lrange")
    List<String> getDataFromList(String key,int start,int end){
        return iCacheService.getDateFromList(key, start, end);
    }
//  从list 中一次性删除多个数据
    @PostMapping("/lremove")
    Long lremove( String key, String... values) {
        return iCacheService.lremove(key, values);
    }














}
